<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/8/6
 * Time: 16:37
 */

namespace app\api\model;


use think\Model;

class MyClassroom extends Model
{
    /**
     * 判断用户是否已收藏该课程
     * @param $classroom_id  课程ID
     * @param $user_id  用户ID
     * @return array|false|\PDOStatement|string|Model
     */
    public function ifCollection($classroom_id, $user_id, $field = ''){
        $res = $this->field($field)
            ->where('user_id', $user_id)
            ->where('classroom_id', $classroom_id)
            ->find();
        return $res;
    }
    
    /**
     * 新增一条收藏记录
     * @param $data 新增的数据
     * @return false|int
     */
    public function addCollect($data){
        $res = $this->allowField(true)->save($data);
        return $res;
    }
    
    /**
     * 删除取消收藏的记录
     * @param $classroom_id  课程ID
     * @param $user_id  用户ID
     * @return int
     */
    public function delCollect($classroom_id, $user_id){
        $res = $this->where('classroom_id', $classroom_id)->where('user_id', $user_id)->delete();
        return $res;
    }
    
    /**
     * 如果有收藏，则更新
     * @param $id  收藏的ID
     * @param $data 更新的数据
     * @return $this
     */
    public function updateCollect($id, $data){
        $res = $this->where('id', $id)->update($data);
        return $res;
    }
}